import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { EventProvider } from '../../providers/event/event';
import { CodepushProvider } from '../../providers/codepush/codepush';
/**
 * Generated class for the Center page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
	selector: 'page-center',
	templateUrl: 'center.html',
})
export class CenterPage {

	//必须静态常量来传递
	private static thatPage: any;

	public centerInfo = {
		name: '',
		office: '',
		photo: ''
	};

	constructor(
		public navCtrl: NavController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		public dialog: DialogProvider,
		public event: EventProvider,
		public codepush: CodepushProvider) { }

	/*界面加载*/
	ionViewDidLoad() {
		console.log('ionViewDidLoad Center');
		CenterPage.thatPage = this;
		//绑定事件
		this.event.bindEvents(this.event.EVENT_CENTER, this.eventPage);
		//刷新数据
		this.event.sendEvents(this.event.EVENT_CENTER, {});
	}
	/*侦听事件 */
	eventPage(val: any) {
		//刷新数据
		CenterPage.thatPage.loadData();
	}
	/*加载数据*/
	loadData() {
		this.storage.getStorage("user").then(val => {
			if (val != undefined) {
				//请求方式如下
				let params = {
					user_id: val['user_id']
				};
				//网络请求
				this.http.postJson('user', 'douserinfo.do', params).then(res => {
					if (res['success'] == true) {
						let info = res['data'];
						//请求成功
						this.centerInfo.name = info['name'];
						this.centerInfo.office = info['office'] != undefined ? info['office'].name : '';
						this.centerInfo.photo = info['photo'] != undefined ? (this.http.APP_HTTP_URL + '/' + info['photo']) : '';
					} else {
						this.dialog.showToast("top", res['msg']);
					}
				});
			}
		});
	}

	/*界面销毁*/
	ionViewWillUnload() {
		this.event.unbindEvents(this.event.EVENT_CENTER, this.eventPage);
	}

	/*个人信息*/
	openuserinfo() {
		this.navCtrl.push("UserinfoPage");
	}

	/*个人工资*/
	openuserwages() {
		this.navCtrl.push("UserwagesPage");
	}

	/*个人领用*/
	openuseruse() {
		this.navCtrl.push("UserusePage");
	}

	/*个人考勤*/
	openuserwork() {
		this.navCtrl.push("UserworkPage");
	}

	/*修改密码*/
	openuserpassedit() {
		this.navCtrl.push("UserpasseditPage");
	}

	/*注销登录*/
	openloginout() {
		//确认对话框
		this.dialog.showConfirm('确认', '确定要退出登录吗?').then(success => {
			this.storage.removeStorage("user");
			this.event.sendEvents(this.event.EVENT_LOGIN, {});
		}, fail => {
			this.event.sendEvents(this.event.EVENT_CENTER, {});
		});
	}

	/*关于我们*/
	openaboutus() {
		this.navCtrl.push("AboutusPage");
	}

	/*版本更新*/
	opencheckupdate() {
		this.dialog.showToast('top', '正在检查更新,请稍后...');
		this.codepush.initCodePush(false);
	}
}